package com.hankkin.reading.base

/**
 * Created by huanghaijie on 2018/6/8.
 */
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.support.annotation.ColorInt
import com.hankkin.library.mvp.contract.IPresenterContract
import com.hankkin.library.mvp.view.MvpActivity
import com.hankkin.reading.event.EventMap

import com.zhsm.paipaiout.baselibrary.utils.*
import com.zhsm.fuxi.R
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.Consumer
import org.devio.takephoto.app.TakePhoto
import org.devio.takephoto.app.TakePhotoImpl
import org.devio.takephoto.compress.CompressConfig
import org.devio.takephoto.model.InvokeParam
import org.devio.takephoto.model.TContextWrap
import org.devio.takephoto.model.TResult
import org.devio.takephoto.permission.InvokeListener
import org.devio.takephoto.permission.PermissionManager
import org.devio.takephoto.permission.TakePhotoInvocationHandler
import java.io.File

abstract class TakePhotoMvpActivity<out P : IPresenterContract> : MvpActivity<P>(), TakePhoto.TakeResultListener, InvokeListener {

    val takePhoto: TakePhoto by lazy {
        TakePhotoInvocationHandler.of(this).bind(TakePhotoImpl(this, this)) as TakePhoto
    }
    var invokeParam: InvokeParam? = null
    var disposables = CompositeDisposable()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        init(savedInstanceState)
        LoadingUtils.onDestory()
        takePhoto.onCreate(savedInstanceState)
        initView()
        registerEvent()
        initData()
    }

    open fun isHasBus(): Boolean {
        return false
    }

    protected fun registerEvent() {
        if (isHasBus()) {
            val disposable = RxBusTools.getDefault().register(EventMap.BaseEvent::class.java, Consumer { onEvent(it) })
            disposables.add(disposable)
        }
    }

    open fun onEvent(event: EventMap.BaseEvent) {
    }

    abstract fun getLayoutId(): Int

    open fun init(savedInstanceState: Bundle?) {}

    open fun initView() {}

    open fun initData() {}


    /**
     * 设置状态栏颜色
     *
     * @param color
     */
    protected fun setStatusBarColor(@ColorInt color: Int) {
        MyStatusBarUtil.setColorForSwipeBack(this, color, 0)
    }


    /**
     * 设置白底黑字
     */
    protected fun setMiuiStatusBar() {
        MyStatusBarUtil.setColorForSwipeBack(this, resources.getColor(R.color.white), 0)
        StatusBarUtil.setLightMode(this)
    }


    private  var file: File? =null
    private var imageUri: Uri? =null

    //去拍照
     fun takePicture() {
        file = File(Environment.getExternalStorageDirectory(), "/paipai/" + System.currentTimeMillis() + ".jpg")
        if (!file!!.getParentFile().exists()) {
            file!!.getParentFile().mkdirs()
        }
        imageUri = Uri.fromFile(file)
        val compressConfig = CompressConfig.Builder().setMaxSize(50 * 1024).setMaxPixel(800).create()
        takePhoto.onEnableCompress(compressConfig, true)
        takePhoto.onPickFromCapture(imageUri)
    }

    fun chosePicture(limit:Int){
        val compressConfig = CompressConfig.Builder().setMaxSize(50 * 1024).setMaxPixel(800).create()
        takePhoto.onEnableCompress(compressConfig, true)
        takePhoto.onPickMultiple(limit)
    }
    override fun takeSuccess(result: TResult?) {

    }

    override fun takeCancel() {
    }

    override fun takeFail(result: TResult?, msg: String?) {
        ToastUtils.makeText("获取图片失败")
    }

    override fun invoke(invokeParam: InvokeParam?): PermissionManager.TPermissionType {
        val type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam!!.method)
        if (PermissionManager.TPermissionType.WAIT == type) {
            this.invokeParam = invokeParam
        }
        return type
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        takePhoto.onActivityResult(requestCode, resultCode, data)
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        takePhoto.onSaveInstanceState(outState)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        val type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionManager.handlePermissionsResult(this, type, invokeParam, this)
    }
    override fun onDestroy() {
        super.onDestroy()
        LoadingUtils.onDestory()
        disposables.clear()
    }

}